//
//  ZGSDataBaseHandle.m
//  Tour
//
//  Created by dllo on 15/11/2.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "ZGSDataBaseHandle.h"

@implementation ZGSDataBaseHandle
+ (instancetype)shareDataBaseHandle{
    static ZGSDataBaseHandle *handle;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handle = [[ZGSDataBaseHandle alloc] init];
        
    });
    [handle openDB];
    
    return handle;
}

- (void)openDB {
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES) lastObject];
    NSString *dbPath = [sandBoxPath stringByAppendingPathComponent:@"Collect.sqlite"];
    int result = sqlite3_open([dbPath UTF8String], &dbPoint);
    if (result == SQLITE_OK) {
        NSLog(@"打开成功");
    }else{
        NSLog(@"打开失败");
    }
    NSLog(@"%@", dbPath);
}

- (void)createTable{
    NSString *sqlstr = @"create table if not exists Collect(countryid text, cnname text)";
    //执行
    int result = sqlite3_exec(dbPoint, [sqlstr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"创建成功");
    }else{
        NSLog(@"创建失败");
    }
}

- (SelectMovie) isHaveIntable:(ZGSQQQQQQQQQQ *)movie{
//    select* from Collect where cnname = '%@'
    NSString *sqlStr = [NSString stringWithFormat:@"select * from Collect where cnname = '%@'", movie.cnname];
    //跟随指针
    sqlite3_stmt *stmt = nil;
    //执行
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    NSLog(@"%d", result);
    if (result == SQLITE_OK) {
        NSLog(@"查询成功");
        if (sqlite3_step(stmt) == SQLITE_ROW) {
            sqlite3_finalize(stmt);
            return Intable;
        }else{
            sqlite3_finalize(stmt);
            return NotInTable;
        }
    }else{
        NSLog(@"查询失败");
        sqlite3_finalize(stmt);
        return SelectError;
    }
    return YES;
}

- (void)saveMovieInDB:(ZGSQQQQQQQQQQ *)movie{
//    insert into Collect (countryid, cnname, photo) values('%@', '%@', '%@')
    NSString *sqlStr = [NSString stringWithFormat:@"insert into Collect (countryid, cnname) values('%@', '%@')", movie.countryid, movie.cnname];
    NSLog(@"%@", sqlStr);
    NSLog(@"123231123%@", movie.countryid);
    //执行
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"添加成功");
    }else{
        NSLog(@"添加失败");
    }
}

- (void)deleteMovieInDB:(ZGSQQQQQQQQQQ *)movie{
    NSString *sqlStr = [NSString stringWithFormat:@"delete from Collect where countryid = '%@'", movie.countryid];
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败");
    }
}


- (void)deleteLuoJiMovieInDB:(ZGSQQQQQQQQQQ *)movie{
    NSString *sqlStr = [NSString stringWithFormat:@"ipdate Collect set flag = '0' where countryid = '%@'", movie.countryid];
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败");
    }
}



- (NSMutableArray *)selectAllMovie{
    NSMutableArray *movieArr = [NSMutableArray array];
    NSString *sqlStr = [NSString stringWithFormat:@"select * from Collect"];
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    if (result == SQLITE_OK) {
        NSLog(@"查询成功");
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            ZGSQQQQQQQQQQ *movie = [[ZGSQQQQQQQQQQ alloc] init];
//
//            NSString *str = [NSString stringWithFormat:@"%@", movie.countryid];
            movie.countryid = [NSString stringWithUTF8String:(const char*)sqlite3_column_text(stmt, 0)];
            movie.cnname = [NSString stringWithUTF8String:(const char*)sqlite3_column_text(stmt, 1)];
            NSLog(@"%@", movie.cnname);
            [movieArr addObject:movie];
            [movie release];
        }
    }else{
        NSLog(@"查询失败");
    }
    return movieArr;
}

@end
